package com.jr.service.impl;

import com.jr.dao.ISupplierWhiteListDao;
import com.jr.dao.impl.SupplierWhiteListDaoImpl;
import com.jr.pojo.SupplierWhiteList;
import com.jr.service.ISupplierWhiteListService;
import com.jr.util.PageHelper;

import java.util.ArrayList;

public class SupplierWhiteListServciceImpl implements ISupplierWhiteListService {
    private ISupplierWhiteListDao iswd = new SupplierWhiteListDaoImpl();

    // 1. 添加供应商到白名单
    @Override
    public boolean addWhiteList(SupplierWhiteList whiteList) {
        return iswd.insertWhiteList(whiteList)>0;
    }

    // 分页查询企业的白名单列表
    @Override
    public PageHelper getWhiteListByPage(Integer enterpriseId,Integer pageIndex) {
        PageHelper ph = new PageHelper();
        // 1. 查询总条数
        int totalCount = iswd.selectWhiteListCount(enterpriseId);
        ph.setDataCount(totalCount);
        // 2. 设置当前页
        if (pageIndex != null) {
            ph.setPageIndex(pageIndex);
        }
        // 3. 计算起始索引
        int start = (pageIndex - 1) * 5;
        // 4. 查询当前页数据
        ArrayList<SupplierWhiteList> whiteList = iswd.selectWhiteListByEnterpriseId(enterpriseId,pageIndex);
        ph.setListIndex(whiteList);
        return ph;

    }

    // 删除白名单
    @Override
    public boolean delBlackList(Integer enterpriseId, Integer supplierId) {
        return iswd.deleteWhiteList(enterpriseId, supplierId)>0;
    }

    //   检查企业是否已在白名单中
    @Override
    public boolean isInWhiteList(Integer enterpriseId, Integer supplierId) {
        return iswd.checkInWhiteList(enterpriseId,supplierId);
    }
    // 按条件分页查询白名单（企业名称模糊查询、企业评级）
    @Override
    public PageHelper searchWhiteListByCondition(Integer enterpriseId, String supplierName, String supplierLevel,Integer pageIndex) {
        PageHelper ph1 = new PageHelper();
        //获取总条数
        int dataCount = iswd.selectWhiteListCount(enterpriseId);
        //根据企业id查供应商白名单数据
        ArrayList<SupplierWhiteList> supplierWhiteLists = iswd.selectWhiteListByCondition(enterpriseId, supplierName, supplierLevel,pageIndex);
        ph1.setDataCount(dataCount);
        ph1.setListIndex(supplierWhiteLists);

        if (pageIndex != null) {
            //获取当前页
            ph1.setPageIndex(pageIndex);
        }
        return ph1;
    }
}
